<template>
  <div class="login-wrap">
    <div class="login-title">Gigi music 后台管理登录</div>
    <div class="login-in">
      <el-form :model="loginFormData" :rules="rules" ref="loginFormData">
        <el-form-item prop="name" class="is-required">
          <el-input suffix-icon="el-icon-user" v-model="loginFormData.name" placeholder="用户名"/>
        </el-form-item>
        <el-form-item prop="pwd" class="is-required">
          <el-input suffix-icon="el-icon-lock" type="password" v-model="loginFormData.pwd" placeholder="密码"/>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {login} from "../api/index";

export default {
  data: function () {
    return {
      loginFormData: {
        name: "Gigi",
        pwd: "123456"
      },
      rules: {
        name: [
          {required: true, message: "请输入用户名", trigger: "blur"}
        ],
        pwd: [
          {required: true, message: "请输入密码", trigger: "blur"}
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginFormData.validate(isValid => {
        if (isValid) {
          login(this.loginFormData).then((res) => {
            if (res.success) {
              // h5：把名字放入缓存，首页时获取
              localStorage.setItem('userName', this.loginFormData.name);
              this.$router.push("/Info");
              this.$notify({
                title: '欢迎回来！',
                type: 'success'
              });
            } else {
              this.$notify({
                title: '用户名或密码错误！',
                type: 'error'
              });
            }
          });
        }
      });
    }
  }
}
</script>

<style>
.login-wrap {
  background: url("../assets/img/background.jpg");
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  position: absolute;
  background-attachment: fixed;
}

.login-title {
  position: relative;
  top: 28%;
  text-align: center;
  font-size: 30px;
  color: white;
  font-weight: bolder;
  width: 100%;
}

.login-in {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 160px;
  margin-left: -190px;
  margin-top: -100px;
  padding: 40px;
  border-radius: 5px;
  background: white;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}
</style>